package com.ruibang.glass.equipment.service.impl.device;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.Constants;
import com.ruibang.glass.equipment.domain.req.device.PointCheckRwReq;
import com.ruibang.glass.equipment.entity.device.PointCheckRecord;
import com.ruibang.glass.equipment.entity.device.PointCheckRw;
import com.ruibang.glass.equipment.mapper.device.PointCheckRecordMapper;
import com.ruibang.glass.equipment.mapper.device.PointCheckRwMapper;
import com.ruibang.glass.equipment.service.PointCheckRwService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 点检任务服务类
 * </p>
 *
 * @author mameng
 * @since 2024-03-20
 */

@Slf4j
@Service
public class PointCheckRwServiceImpl extends ServiceImpl<PointCheckRwMapper, PointCheckRw> implements PointCheckRwService {
    @Autowired
    private PointCheckRecordMapper pointCheckRecordMapper;

    /**
     * 点检任务列表
     * @param deviceAllPageReq
     * @return
     */
    @Override
    public IPage<PointCheckRw> getCheckPlanList(PointCheckRwReq deviceAllPageReq) {
        LambdaQueryWrapper<PointCheckRw> queryWrapper = Wrappers.<PointCheckRw>lambdaQuery();
        if (StringUtils.isNotBlank(deviceAllPageReq.getDeviceName())) {
            queryWrapper.like(PointCheckRw::getDeviceName, deviceAllPageReq.getDeviceName());
        }
        if (StringUtils.isNotBlank(deviceAllPageReq.getPointPart())) {
            queryWrapper.like(PointCheckRw::getPointPart, deviceAllPageReq.getPointPart());
        }
        if (StringUtils.isNotBlank(deviceAllPageReq.getPointSpeciality())) {
            queryWrapper.like(PointCheckRw::getPointSpeciality, deviceAllPageReq.getPointSpeciality());
        }
        if (StringUtils.isNotBlank(deviceAllPageReq.getPointPerson())) {
            queryWrapper.like(PointCheckRw::getPointPerson, deviceAllPageReq.getPointPerson());
        }

        return baseMapper.selectPage(new Page<>(deviceAllPageReq.getPageNo(), deviceAllPageReq.getPageSize()),
                queryWrapper.orderByDesc(PointCheckRw::getCreateTime)
        );
    }

    /**
     * 检修任务
     * @param point
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultBody addOrUpdateCheck(PointCheckRw point) {
        PointCheckRw pointCheckPlan = baseMapper.selectById(point.getId());
        PointCheckRecord pointCheckRecord = new PointCheckRecord();
        pointCheckRecord.setDeviceName(pointCheckPlan.getDeviceName());
        pointCheckRecord.setInspectionLocation(pointCheckPlan.getPointPart());
        pointCheckRecord.setInspectionTime(pointCheckPlan.getStartTime());
        pointCheckRecord.setInspector(pointCheckPlan.getPointPerson());
        pointCheckRecord.setPlanId(pointCheckPlan.getId());
        pointCheckRecord.setStartTime(point.getInspectionTime());
        pointCheckRecord.setProfessional(pointCheckPlan.getPointSpeciality());
        pointCheckRecord.setResult(point.getResult());
        pointCheckRecordMapper.insert(pointCheckRecord);
        baseMapper.updateById(pointCheckPlan);
        return ResultBody.success();
    }





}
